package com.ilancuo.thinkinjava.chapter_14.pets;

import java.util.HashMap;

public class PetCount {
    static class PetCounter extends HashMap<String, Integer> {
        public void count(String type) {
            Integer quantity = get(type);
            if (quantity == null) {
                put(type, 1);
            } else {
                put(type, quantity + 1);
            }
        }
    }

    public static void countPets(PetCreator creator) {
        PetCounter counter = new PetCounter();
        for (Pet pet : creator.createArray(20)) {
            System.out.print(pet.getClass().getSimpleName() + " ");
            if (pet instanceof Pet)
                counter.count(Pet.class.getSimpleName());
            if (pet instanceof Dog)
                counter.count(Dog.class.getSimpleName());
            if (pet instanceof Mutt)
                counter.count(Mutt.class.getSimpleName());
            if (pet instanceof Pug)
                counter.count(Pug.class.getSimpleName());
            if (pet instanceof Cat)
                counter.count(Cat.class.getSimpleName());
            if (pet instanceof Manx)
                counter.count(Manx.class.getSimpleName());
            if (pet instanceof EgyptianMau)
                counter.count(EgyptianMau.class.getSimpleName());
            if (pet instanceof Manx)
                counter.count(Manx.class.getSimpleName());
            if (pet instanceof Cymric)
                counter.count(Cymric.class.getSimpleName());
            if (pet instanceof Rodent)
                counter.count(Rodent.class.getSimpleName());
            if (pet instanceof Rat)
                counter.count(Rat.class.getSimpleName());
            if (pet instanceof Mouse)
                counter.count(Mouse.class.getSimpleName());
            if (pet instanceof Hamster)
                counter.count(Hamster.class.getSimpleName());
        }
        System.out.println();
        System.out.println(counter);
    }

    public static void main(String[] args) {
        countPets(new ForNameCreator());
    }
}
